package com.cuitrip.business.tripservice;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.SpanUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.widget.WordCountView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by sheena on 16/4/22.
 */
public class CreateTripDetailFragment extends CustomUiFragment {

    public CreateTripDescFragment.OnResultCallback onResultCallback;
    @Bind(R.id.desc)
    TextView desc;
    @Bind(R.id.wcv_detail)
    WordCountView wcvDetail;
    private ServiceInfo serviceInfo;

    public static CreateTripDetailFragment newInstance(ServiceInfo serviceInfo) {
        CreateTripDetailFragment createTripDetailFragment = new CreateTripDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ServiceInfo.KEY, serviceInfo);
        createTripDetailFragment.setArguments(bundle);
        return createTripDetailFragment;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mLeftStyle = CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.trip_content_detail);
        return config;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_create_trip_detail);
        if (getArguments() != null) {
            serviceInfo = (ServiceInfo) getArguments().getSerializable(ServiceInfo.KEY);
        }
        return view;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this, getView());
        if (serviceInfo != null) {
//            wcvDetail.setText(serviceInfo.getDescptWithnoPic());
            wcvDetail.setText(serviceInfo.getServiceDescript());
        }
    }


    @Override
    public void initListener() {
        SpannableStringBuilder epJump;
        if (serviceInfo.getCateList()==null || serviceInfo.getCateList().size()==0){
            epJump = SpanUtil.getEPJump(getString(R.string.trip_content_detail_desc_2), getString(R.string.trip_content_intro_see_example), getHostActivity(), SpanUtil.TRIP_DETAIL_TYPE, "");
        }else {
            epJump = SpanUtil.getEPJump(getString(R.string.trip_content_detail_desc_2), getString(R.string.trip_content_intro_see_example), getHostActivity(), SpanUtil.TRIP_DETAIL_TYPE, serviceInfo.getCateList().get(0).getCategory());
        }
        desc.setText(epJump);
        desc.setClickable(true);
        desc.setMovementMethod(new LinkMovementMethod() {
            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                super.onTakeFocus(view, text, dir);
            }
        });
    }

    private void saveAction() {
        String content = wcvDetail.getContent();
//        serviceInfo.setDescptWithNoPic(content);
        serviceInfo.setServiceDescript(content);
        onResultCallback.onResultBack(serviceInfo);

    }

    public void setResultCallBack(CreateTripDescFragment.OnResultCallback onResultCallback) {
        this.onResultCallback = onResultCallback;
    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed(false);
    }


    @Override
    public boolean onBackPressed(boolean fromKey) {
        saveAction();
        return super.onBackPressed(fromKey);
    }
}
